package com.xzy.urp.service.mq;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xzy.urp.pojo.entity.CourseInfo;
import com.xzy.urp.service.CourseInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class MQReceiver {

    private final RabbitTemplate rabbitTemplate;
    CourseInfoService courseInfoService;

    public MQReceiver(RabbitTemplate rabbitTemplate, CourseInfoService courseInfoService) {
        this.rabbitTemplate = rabbitTemplate;
        this.courseInfoService = courseInfoService;
    }


    @RabbitListener(queues = "incrQueue")
    public void incrTicket(Long cid){
        log.info("cid库存自增");
        courseInfoService.update(new UpdateWrapper<CourseInfo>().eq("id",cid).
                setSql("surplus=surplus+1"));


    }
    @RabbitListener(queues = "decrQueue")
    public void decrTicket(Long cid){
        log.info("cid库存自减");
        courseInfoService.update(new UpdateWrapper<CourseInfo>().eq("id",cid).
                setSql("surplus=surplus-1"));
    }



}
